package cn.xo68.ide.plugin.common.jobs;

import cn.xo68.ide.plugin.common.git.GitClient;
import cn.xo68.ide.plugin.common.git.GitRun;
import cn.xo68.ide.plugin.common.job.JobCallback;
import cn.xo68.ide.plugin.common.job.JobCallbackAdapter;
import cn.xo68.ide.plugin.common.job.ToolSuiteJob;
import com.intellij.openapi.progress.ProgressIndicator;

import java.util.List;

public class GitJob implements ToolSuiteJob {

    private final GitClient gitClient;
    private final List<GitRun> gitRuns;
    private final String title;
    private JobCallback jobCallback;
    
    public GitJob(String title, GitClient gitClient, List<GitRun> gitRuns) {
        this.gitClient=gitClient;
        this.gitRuns=gitRuns;
        this.title = title;
        jobCallback=new JobCallbackAdapter(title);
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public boolean isModal() {
        return false;
    }

    @Override
    public boolean isStartInBackground() {
        return true;
    }

    @Override
    public JobCallback getCallback() {
        return jobCallback;
    }

    public void setJobCallback(JobCallback jobCallback) {
        this.jobCallback = jobCallback;
    }

    @Override
    public void execute(ProgressIndicator indicator)  throws Exception{
        for (GitRun gitRun : gitRuns) {
            gitRun.run(gitClient);
            getCallback().progress();
        }

        gitClient.close();
    }
}
